/*-----------------------------------------------------------------------*/
/* Low level disk I/O module SKELETON for FatFs     (C)ChaN, 2025        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control modules to the FatFs module with a defined API.       */
/*-----------------------------------------------------------------------*/

#include <stdio.h>
#include "sdcard.h"
#include "ff.h"			/* Basic definitions of FatFs */
#include "diskio.h"		/* Declarations FatFs MAI */

/* Example: Declarations of the platform and disk functions in the project */
//#include "platform.h"
//#include "storage.h"

/* Example: Mapping of physical drive number for each drive */
#define SDCARD 0
//#define DEV_FLASH	0	/* Map FTL to physical drive 0 */
//#define DEV_MMC		1	/* Map MMC/SD card to physical drive 1 */
//#define DEV_USB		2	/* Map USB MSD to physical drive 2 */

sd_card_info_struct sd_cardinfo;
/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
    BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{
    DSTATUS stat;
    int result;


    if(pdrv == SDCARD) return RES_OK;
    else {
        printf("[FAT_FS]: disk_status ERR\r\n");


        return RES_PARERR;
    }
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
    BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{
    if (pdrv == SDCARD)
    {
        sd_error_enum status = SD_OK;
        uint32_t cardstate = 0;
        status = sd_init();
        if(SD_OK == status) {
            status = sd_card_information_get(&sd_cardinfo);
        }
        if(SD_OK == status) {
            status = sd_card_select_deselect(sd_cardinfo.card_rca);
        }
        status = sd_cardstatus_get(&cardstate);
        if(cardstate & 0x02000000) {
            printf("\r\n the card is locked!");
            while(1) {
            }
        }
        if((SD_OK == status) && (!(cardstate & 0x02000000))) {
            /* set bus mode */
            status = sd_bus_mode_config(SDIO_BUSMODE_4BIT);
            //        status = sd_bus_mode_config(SDIO_BUSMODE_1BIT);
        }
        if(SD_OK == status) {
            /* set data transfer mode */
            status = sd_transfer_mode_config(SD_DMA_MODE);
            // status = sd_transfer_mode_config(SD_POLLING_MODE);
        }

        if (SD_OK == status)
        {
            return RES_OK;
        }
        else
        {
            printf("SDCARD init failed [%d]\n", status);
            return RES_PARERR;
        }
    } else
    {
        printf("[FAT_FS]: disk_initialize ERR\r\n");
        return RES_PARERR;
    }
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
    BYTE pdrv,		/* Physical drive nmuber to identify the drive */
    BYTE *buff,		/* Data buffer to store read data */
    LBA_t sector,	/* Start sector in LBA */
    UINT count		/* Number of sectors to read */
)
{
    if (pdrv == SDCARD)
    {
        sd_block_read((uint32_t *)buff, sector * 512, count * 512);
        return RES_OK;
    } else
    {
        printf("[FAT_FS]: disk_read ERR\r\n");
        return RES_PARERR;
    }
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write (
    BYTE pdrv,			/* Physical drive nmuber to identify the drive */
    const BYTE *buff,	/* Data to be written */
    LBA_t sector,		/* Start sector in LBA */
    UINT count			/* Number of sectors to write */
)
{
    if (pdrv == SDCARD)
    {
        sd_block_write((uint32_t *)buff, sector * 512, count * 512);
        return RES_OK;
    } else
    {
        printf("[FAT_FS]: disk_write ERR\r\n");
        return RES_PARERR;
    }
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
    BYTE pdrv,		/* Physical drive nmuber (0..) */
    BYTE cmd,		/* Control code */
    void *buff		/* Buffer to send/receive control data */
)
{
    DRESULT res;
    int result;

    switch (pdrv)
    {
    case SDCARD:
        switch(cmd)
        {
        case CTRL_SYNC:
            return RES_OK;
        case GET_SECTOR_COUNT:
            *(uint32_t * )buff = 247808;
            return RES_OK;
        case GET_SECTOR_SIZE:
            *(uint16_t * )buff = 512;      // SD��һ������512Byte
            return RES_OK;
        case GET_BLOCK_SIZE:
            *(uint32_t * )buff = 1;        // һ��block�ж��ٸ����������ָ���Ǳ���Ĩ���Ȳ�������Ĩ������������ȷ��д1ҲOK
            return RES_OK;
        }

    default:
        return RES_PARERR;      // �����������޷�ƥ�䵽case����ʾ�������󷵻ز�������
    }

    return RES_PARERR;
}

